package com.wuyan.locationdog.ui.my


import android.graphics.Color
import androidx.activity.viewModels
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.ext.customShowLong
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.textStringTrim
import com.arcsoft.commonbase.ext.visible
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.databinding.OrderDetailActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.ui.device.AddSuccessActivity
import com.wuyan.locationdog.view.PayPopup
import com.wuyan.locationdog.viewmodel.MyViewModel

class MyOrderDetailActivity : BaseBindingActivity<OrderDetailActivityBinding>() {
    private val mViewModel: MyViewModel by viewModels()

    override fun initData() {
        setTitleText("订单详情")

        binding.apply {
            if (DeviceHelp.instantOrderListBeanContent.orderStatus == 1) {
                orderStatus.text = "待付款"

                val longTime = TimeUtils.string2Millis(DeviceHelp.instantOrderListBeanContent.createTime)

                orderStatusDesc.text = TimeUtils.millis2String(longTime + 30 * 1000 * 60 ) + "后自动取消"

                shapeBg.setShapeGradientType(0)
                    .setShapeGradientOrientation(4)//BOTTOM_TOP
                    .setShapeGradientStartColor(Color.parseColor("#A784FA"))
                    .setShapeGradientEndColor(Color.parseColor("#6E4DF3"))
                    .setUseShape()

                bottomLayout.visible()

            } else if (DeviceHelp.instantOrderListBeanContent.orderStatus == 5) {
                orderStatus.text = "已完成"
                orderStatusDesc.text = "订单已完成，欢迎下次购买"

                shapeBg.setShapeGradientType(0)
                    .setShapeGradientOrientation(4)
                    .setShapeGradientStartColor(Color.parseColor("#56D6BA"))
                    .setShapeGradientEndColor(Color.parseColor("#2BAD84"))
                    .setUseShape()
                bottomLayout.gone()

            } else {
                orderStatus.text = "已取消"
                orderStatusDesc.text = "订单已取消，欢迎下次购买"
                shapeBg.setShapeGradientType(0)
                    .setShapeGradientOrientation(4)
                    .setShapeGradientStartColor(Color.parseColor("#BFBFBF"))
                    .setShapeGradientEndColor(Color.parseColor("#8C8C8C"))
                    .setUseShape()
                bottomLayout.gone()

            }

            orderNo.text = DeviceHelp.instantOrderListBeanContent.orderNo
            imei.text = DeviceHelp.instantOrderListBeanContent.imei
            createTime.text = DeviceHelp.instantOrderListBeanContent.createTime
            DeviceHelp.instantOrderListBeanContent.orderVirtualServiceList.first().let {
                orderCommodityNum.text = "x" + it.orderCommodityNum
                retailUnitPrice.text = "¥" + it.retailUnitPrice
                retailTotalPrice.text = "¥" + it.retailTotalPrice
                retailTotalPrice2.text = "¥" + it.retailTotalPrice

                packageDesc.text = it.packageDesc
            }

            btnOrderNo.setOnClickListener {
                ClipboardUtils.copyText(orderNo.textStringTrim())
                customShowLong("复制成功")
            }
            btnUserCode.setOnClickListener {
                ClipboardUtils.copyText(userCode.textStringTrim())
                customShowLong("复制成功")
            }
            btnImei.setOnClickListener {
                ClipboardUtils.copyText(imei.textStringTrim())
                customShowLong("复制成功")
            }
            btnSure.setOnClickListener {
                val payPopup = PayPopup(mActivity, DeviceHelp.instantOrderListBeanContent.orderVirtualServiceList.first().retailTotalPrice) {
                    val map = HashMap<String, Any>()
                    map["orderId"] = DeviceHelp.instantOrderListBeanContent.orderId
                    map["payType"] = it
                    mViewModel.pay(map)
//                    ActivityUtils.startActivity(AddSuccessActivity::class.java)
                }.showPopupWindow()

            }
        }
    }


    override fun loadNetData() {
        super.loadNetData()

        val map = HashMap<String, Any>()
        map["orderId"] = DeviceHelp.instantOrderListBeanContent.orderId
        mViewModel.myOrderDetail(map)
    }

    override fun observe() {
        super.observe()
        mViewModel.orderListDetailBean.observe(this) {

            binding.apply {
                userCode.text = it.content.userCode
            }

        }
        mViewModel.success.observe(this) {
            ActivityUtils.startActivity(AddSuccessActivity::class.java)
        }
    }

}